package com.ddwl.gate.mq.receive;

import com.alibaba.fastjson.JSONObject;
import com.ddwl.common.config.DingTalkNotify;
import com.ddwl.common.constant.RabbitMqConstants;
import com.ddwl.gate.service.DingTalkService;
import com.ddwl.schema.bo.gate.DingTalkBo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


/**
 * 类RabbitMqListener.java的实现描述：消息队列
 *
 * @author zach
 * @Date :2019-05-08 14:55.
 */
@Component
@Slf4j
public class DingTalkMqMessageReceiver {

    @Resource
    private DingTalkService dingTalkService;
    @Resource
    private DingTalkNotify dingTalkNotify;

    @RabbitListener(bindings ={@QueueBinding(value = @Queue(value = RabbitMqConstants.DINGDING_TALK_MESSAGE,durable = "true"),
            exchange =@Exchange(value = RabbitMqConstants.ORDER_EXCHANGE),key = RabbitMqConstants.DINGDING_TALK_MESSAGE)})
    public void orderDistFinish(String dingTalk) {
        log.info("开始实时推送钉钉消息 dingTalk={}",dingTalk);
        try {
            dingTalkService.sendMessageNow(JSONObject.parseObject(dingTalk, DingTalkBo.class));
        }catch (Exception e){
            log.error( "实时推送钉钉消息异常 dingTalk ={} ",dingTalk,e);
            dingTalkNotify.exceptionNotify(e);
        }
    }

}
